package com.frcdi.qms.equipment.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.utils.SecurityUtils;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.equipment.domain.EquipmentCycle;
import com.frcdi.qms.equipment.mapper.EquipmentCycleMapper;
import com.frcdi.qms.masterdata.domain.ChangeExecution;
import com.frcdi.qms.masterdata.mapper.ChangeExecutionMapper;
import com.frcdi.qms.equipment.mapper.EquipmentBaseMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.frcdi.qms.equipment.domain.EquipmentBase;
import com.frcdi.qms.equipment.service.IEquipmentBaseService;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

/**
 * 设备基础信息Service业务层处理
 *
 * @author cell
 * @date 2023-12-07
 */
@Service
public class EquipmentBaseServiceImpl implements IEquipmentBaseService
{
    @Autowired
    private EquipmentBaseMapper equipmentBaseMapper;

    @Autowired
    private EquipmentCycleMapper cycleMapper;

    @Autowired
    private ChangeExecutionMapper executionMapper;

    /**
     * 查询设备基础信息
     *
     * @param equipmentId 设备基础信息主键
     * @return 设备基础信息
     */
    @Override
    public EquipmentBase selectEquipmentBaseByEquipmentId(Long equipmentId)
    {
        EquipmentBase equipmentBase = equipmentBaseMapper.selectEquipmentBaseByEquipmentId(equipmentId);
        return equipmentBase;
    }

    /**
     * 查询设备基础信息列表
     *
     * @param equipmentBase 设备基础信息
     * @return 设备基础信息
     */
    @Override
    public List<EquipmentBase> selectEquipmentBaseList(EquipmentBase equipmentBase)
    {
        return equipmentBaseMapper.selectEquipmentBaseList(equipmentBase);
    }

    /**
     * 新增设备基础信息
     *
     * @param equipmentBase 设备基础信息
     * @return 结果
     */
    @Override
    public String insertEquipmentBase(EquipmentBase equipmentBase)
    {
        equipmentBaseMapper.insertEquipmentBase(equipmentBase);
        if (StringUtils.isNotEmpty(equipmentBase.getEquipmentCycle())){
            // 新增设备生命周期管理
            insertEquipmentCycle(equipmentBase);
        }
        return equipmentBase.getEquipmentId().toString();
    }

    /**
     * 修改设备基础信息
     *
     * @param equipmentBase 设备基础信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEquipmentBase(EquipmentBase equipmentBase)
    {
        if (StringUtils.isNotEmpty(equipmentBase.getEquipmentCycle())){
            // 删除设备与生命周期关联
            cycleMapper.deleteCycleByEquipmentId(equipmentBase.getEquipmentId());
            // 新增设备生命周期管理
            insertEquipmentCycle(equipmentBase);
        }
        return equipmentBaseMapper.updateEquipmentBase(equipmentBase);
    }

    /**
     * 批量删除设备基础信息
     *
     * @param equipmentIds 需要删除的设备基础信息主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBaseByEquipmentIds(Long[] equipmentIds)
    {
        // 删除设备与生命周期关联
        cycleMapper.deleteCycleByEquipmentIds(equipmentIds);

        return equipmentBaseMapper.deleteEquipmentBaseByEquipmentIds(equipmentIds);
    }

    /**
     * 删除设备基础信息信息
     *
     * @param equipmentId 设备基础信息主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentBaseByEquipmentId(Long equipmentId)
    {
        // 删除设备与生命周期关联
        cycleMapper.deleteCycleByEquipmentId(equipmentId);
        return equipmentBaseMapper.deleteEquipmentBaseByEquipmentId(equipmentId);
    }
    @Override
    public boolean checkequipmentBaseNameUnique(EquipmentBase equipmentBase) {
        Long equipmentId = StringUtils.isNull(equipmentBase.getEquipmentId()) ? -1L : equipmentBase.getEquipmentId();
        EquipmentBase info = equipmentBaseMapper.checkequipmentBaseNameUnique(equipmentBase.getEquipmentName(), equipmentBase.getEquipmentType());
        if (StringUtils.isNotNull(info) && info.getEquipmentId().longValue() != equipmentId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    public void insertEquipmentCycle(EquipmentBase equipmentBase)
    {
        Long id = equipmentBase.getEquipmentId();
        String[] cycles = equipmentBase.getEquipmentCycle();
        if (StringUtils.isNotEmpty(cycles))
        {
            // 新增设备与生命周期
            List<EquipmentCycle> list = new ArrayList<EquipmentCycle>(cycles.length);
            for (String equipmentStatus : cycles)
            {
                EquipmentCycle up = new EquipmentCycle();
                up.setEquipmentId(id);
                up.setCycleStatus(equipmentStatus);
                list.add(up);
            }
            cycleMapper.batchCycle(list);
        }
    }

    @Transactional
    @Override
    public int submitApplicant(EquipmentBase equipmentBase) {
        // 修改物料返回状态
        EquipmentBase upEquipment= new EquipmentBase();
        upEquipment.setEquipmentId(equipmentBase.getEquipmentId());
        upEquipment.setSend(UserConstants.SEND_HAS_RETURNED);
        equipmentBaseMapper.updateEquipmentBase(upEquipment);
        // 新增变更执行单
        ChangeExecution execution = new ChangeExecution();
        execution.setChangeNo(equipmentBase.getChangeNo());
        execution.setAssociatedId(equipmentBase.getEquipmentId());
        execution.setAssociatedName(equipmentBase.getEquipmentName());
        execution.setAssociatedType(UserConstants.EQUIPMENT);
        execution.setCreateBy(SecurityUtils.getUsername());
        return executionMapper.insertChangeExecution(execution);
    }
}
